package conexao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Classe copiada ao gerar novo projeto
 * 
 * @author alessandro.silva
 *
 */
public class Conexao {
    public static Connection conn = null;

    public static PreparedStatement pst = null;

    public static ResultSet rs = null;

    /**
     * Metodo que realiza a conexao com o banco de dados
     * 
     * @param base
     * @param local
     * @param user
     * @param senha
     * @return
     * @throws SQLException
     */
    public boolean conectar(String base, String local, String user, String senha)
            throws SQLException {

        //			try {
        //				Class.forName("com.mysql.jdbc.Driver");
        //				conn = DriverManager.getConnection("jdbc:mysql://"+local+"/" + base + "?user="+user+"&password="+senha);
        //			} catch (Exception e) {
        //				return false;
        //			}
        //		return true;
        //	}

        try {
            Class.forName("org.postgresql.Driver");
            conn = DriverManager.getConnection("jdbc:postgresql://" + local
                    + "/" + base + "?user=" + user + "&password=" + senha);
            conn.setAutoCommit(false);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    
    /**
     * Metodo que da rollback 
     * 
     * @throws SQLException
     */
    public static void rollback() throws SQLException{
    	conn.rollback();
    	desconectar();
    	System.out.println("CONEXAO-->rollback");
    }
    
    /**
     * Metodo que da commit
     * 
     * @throws SQLException
     */
    public static void commit() throws SQLException{
    	conn.commit();
    	System.out.println("CONEXAO-->commit");
    }
    
    /**
     * Metodo que desconecta o banco de dados
     * 
     * @return
     */
    public static boolean desconectar() {
        try {
            conn.close();
            System.out.println("CONEXAO-->close");
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;

    }
    
}
